package com.vq.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * <h2>电子邮箱工具</h2>
 */
public class EmailUtil {
    @Autowired
    JavaMailSender javaMailSender;

    String from = "vq202306@163.com(VQ官方邮箱)";//发送人

    /**
     * 发送电子邮箱
     * @param simpleMailMessage 电子邮箱
     * @param isHtml 是否HTML解析
     * @throws MessagingException
     */
    public void send(SimpleMailMessage simpleMailMessage, boolean isHtml) throws MessagingException {
        //需要html解析
        if (isHtml) {
            MimeMessage message = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);//发送人
            helper.setTo(simpleMailMessage.getTo());//接收人
            helper.setSubject(simpleMailMessage.getSubject());//标题
            helper.setText(simpleMailMessage.getText(), true);//内容
            javaMailSender.send(message);
        }
        //普通邮箱
        else {
            javaMailSender.send(simpleMailMessage);
        }
    }
}
